<?php

namespace App\HttpController;
use EasySwoole\EasySwoole\Config;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\Http\AbstractInterface\Controller;
use App\Models\AdminModel;
use App\Models\UserModel;
use EasySwoole\Component\Di;
use App\HttpController\UserBase;
use App\Models\Project;
use App\Models\Level;
use App\Models\Assign;
use App\Models\File;
use App\Models\History;

use App\Models\Industry;
use App\Models\Apptype;

class Admin extends UserBase
{

    function useradd(){
        $isupdate = $this->input("isupdate");
        $who = $this->who;
        $params =  $this->request()->getRequestParam();
        $params['userPassword'] = md5($params['userPassword']);
        $params['addtimestamp'] = time();
        $params['type'] = intval($params['type']);


       
        if($isupdate == 1){
            $res = UserModel::create()->update($params,['id'=>$this->input('id')]);
        }else{
            $isexists = UserModel::create()->all(['userAccount'=>$params['userAccount']]);
            if($isexists){
                $this->writeJson(200,['code'=>500,'msg'=>'该账号已存在']);
                return false;
            }
    
            unset($params['id']);
            $res = UserModel::create($params)->save();
        }
       
        if($res){
            $this->writeJson(200,['code'=>200]);
        }
    }



    function list(){
        $limit = \EasySwoole\EasySwoole\Config::getInstance()->getConf('pagelimit');
        $page = $this->input('p',1);
        $name = $this->input("name","");
        $idcard = $this->input("idcard","");
        $status = $this->input("status",1);
        $addtime = $this->input("addtime","");
        $level = $this->input("level");
    
        $where = [];
       $model =  UserModel::create();
        if($name == 'null'){
            $name = "";
        }
        $type = $this->input('type');

        if($type == 'jg'){
            $model = $model->where(['type'=>[[0,5],'in']]);
        }else{
            $model = $model->where(['type'=>[[0,5],'not in']]);
        }
       
        if($idcard == 'null'){
            $idcard = "";
        }
        if($status == 'null'){
            $status = "";
        }
        if($addtime == 'null'){
            $addtime = "";
        }else{
            $addtime = strtotime($addtime);
            $endtime = $addtime+86400;
        }

        if($name){
            $model = $model->where("displayname","%".$name."%",'like');
        }

        if($idcard){
            $model = $model->where("idcard","%".$idcard."%",'like');
        }
    
        if(in_array($status,[0,1]) && $status!= "" && $status!="all"){

            $model = $model->where(['status'=>$status]);
        }
        

        if($level && $level !="all" && $level != 'null'){
            $model = $model->where(['approve_level'=>$level]);
        }

        if($addtime && $endtime){
            $model = $model->where(["addtimestamp"=>[[$addtime,$endtime],'between']]);
        }


        $model = $model->order('id', 'DESC')->limit($limit * ($page - 1), $limit)->withTotalCount();
        $list = $model->all(null);
        $result = $model->lastQueryResult();
        $total = $result->getTotalCount();
        $pagecon = $this->showPageStringAsAJAXForitem("jumpage",$page,$limit,$total,"d");
        $this->writeJson(200,['list'=>$list,'pg'=>$pagecon]);
    }

    function getdetail(){
        $params = $this->request()->getRequestParam();
        $id = $params['id'];
        if (!$id) {
            $this->writeJson(404,"NOT FOUND ITEM");
        }
        $project = Project::create()->get(["id"=>intval($id)]);
        $files = File::create()->where(['itemid'=>$id])->all();

        $logs = History::create()->join('user_list','user_list.id = approve_history.userid')->all(['approveres'=>0,'itemid'=>intval($id)]);
        $this->writeJson(200,['item'=>$project,'logs'=>$logs,'files'=>$files]);
    }


    function getlevels(){
       $levels =  Level::create()->all(['status'=>1]);
       $industry = Industry::create()->all(['status'=>1]);
       $type = Apptype::create()->all(['status'=>1]);
       $this->writeJson(200,['level'=>$levels,'industry'=>$industry,'type'=>$type]);
    }



    function getowners(){

        $yzs = UserModel::create()->where(['status'=>1,'type'=>2])->column('userAccount');
        $this->writeJson(200,$yzs);


    }


    function  deluser(){


        $id = $this->input("id");
        $res = UserModel::create()->update(['status'=>0],['id'=>intval($id)]);
        if($res){
            $this->writeJson(200,['code'=>200]);
        }



    }
   
}
